#pragma once 
#include <iostream>
using namespace std;
#include <unistd.h>
#include <unordered_map>
class Handler
{
    public:    
    Handler() = default;    
    void operator()(int sock)    
    {    
      // map three words
      unordered_map<string , string> dict;
      dict.insert(make_pair("hello" , "nihao"));
      dict.insert(make_pair("world" , "shijie"));
      dict.insert(make_pair("sort"  , "paixu"));


      char buff[1024];
      string value;

      while(true)
      {
        ssize_t size = read(sock , buff , sizeof(buff) - 1);

        if(size > 0)
        {
          buff[size] = 0;
          cout << "sock: " << sock << endl;

          string key = buff;
          auto it = dict.find(key);
          if (it != dict.end())
          {
            value = it -> second;
          }
          else 
          {
            value = key;
          }

          write(sock , value.c_str() , value.size());

        }
        else if (size == 0)
        {
          cout << "read close" << endl;
          break;
        }
        else
        {
          cout << "unknown error " << endl;
          break;
        }

      }

      close(sock);
      cout << "service end" << endl;
    }
  };

class Task
{
  private:
    int _sock;
    Handler _handler;
  public:
    Task(int sock)
      :_sock(sock)
    {}

    Task() = default;
    void run()
    {
      _handler(_sock);
    }
};
